package com.example.eeuse.elasticsearch.controller;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.SearchHit;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ElasticsearchToJson {

    public static void main(String[] args) {
        String indexName = "us_news_media_china_economy_reception";
        String outputFilePath = "us_news_media_china_economy_reception.json";

        try (RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http")))) {

            // 创建搜索请求
            SearchRequest searchRequest = new SearchRequest(indexName);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
            searchSourceBuilder.size(1000); // 每次滚动查询的数量
            searchRequest.source(searchSourceBuilder);
            searchRequest.scroll(TimeValue.timeValueMinutes(1)); // 设置滚动时间

            // 执行搜索请求
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

            // 检查总命中数
            long totalHits = searchResponse.getHits().getTotalHits().value;
            if (totalHits == 0) {
                System.out.println("No data found in the index.");
                return; // 提前返回
            }

            String scrollId = searchResponse.getScrollId();
            if (scrollId == null || scrollId.isEmpty()) {
                System.out.println("Initial search response: " + searchResponse);
                throw new RuntimeException("Initial scrollId is missing or empty");
            }

            List<Object> allResults = new ArrayList<>();

            do {
                for (SearchHit hit : searchResponse.getHits().getHits()) {
                    allResults.add(hit.getSourceAsMap());
                }
                SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
                scrollRequest.scroll(TimeValue.timeValueMinutes(1)); // 继续滚动
                searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
                scrollId = searchResponse.getScrollId(); // 更新 scrollId
            } while (searchResponse.getHits().getHits().length != 0);

            // 将结果写入JSON文件
            ObjectMapper objectMapper = new ObjectMapper();
            try (FileWriter fileWriter = new FileWriter(outputFilePath)) {
                objectMapper.writerWithDefaultPrettyPrinter().writeValue(fileWriter, allResults);
            }

            System.out.println("数据已成功导出到 " + outputFilePath);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
